package invs.invs300both;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class CardRecordDbHelper extends SQLiteOpenHelper {	
	public int m_iRecPerPage = 14;
	public int m_iTotalPage = 0;
	public int m_iTotalRec = 0;
	public int m_iCurPage = 0;
	//1�����ø��๹����
	public CardRecordDbHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}
	public CardRecordDbHelper(Context context) {
		super(context, "/sdcard/invs300ForReadCard.db", null, 1);
	}
	//����ݿ��״δ���ʱִ�и÷�����һ�㽫������ȳ�ʼ���������ڸ÷�����ִ��.
	//2����дonCreate����������execSQL����������
	public void onCreate(SQLiteDatabase arg0) {

		arg0.execSQL("create table if not exists cardrecord("
				+ "id integer primary key,"
				+ "name varchar,"
				+ "sex varchar,"
				+ "nation varchar,"
				+ "birth varchar,"
				+ "addr varchar,"
				+ "idnum varchar,"
				+ "police varchar,"
				+ "begin varchar,"
				+ "end varchar,"
				+ "time varchar,"
				+ "appaddr varchar,"
				+ "finger BLOB,"
				+ "txt BLOB,"
				+ "wlt BLOB,"
				+ "jpg BLOB,"
				+ "bmp BLOB,"
				+ "wlt1 BLOB)"
				);
	}

	//������ݿ�ʱ����İ汾���뵱ǰ�İ汾�Ų�ͬʱ����ø÷���
	//3����дonUpgrade������sql���ɾ��?���´�����
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		arg0.execSQL("drop table cardrecord");
		onCreate(arg0);
	}

	//4�����뷽��   
	public void insert(ContentValues values){  
	    //���SQLiteDataBaseʵ��   
	    SQLiteDatabase db=getWritableDatabase();  
	    //����   
	    db.insert("cardrecord", null, values);  
	    db.close();  
	}  
	
	//5����ѯ����   
	public Cursor query(){  
	    //��ȡSQLiteDatabaseʵ��   
	    SQLiteDatabase db = getWritableDatabase();  
	    //��ѯ���Cursor   
	    return db.query("cardrecord", null, null, null, null, null, null);  
	} 
	
	public Cursor GetRow(int rownum){  
	    SQLiteDatabase db = getWritableDatabase();    
	    Cursor cur = db.rawQuery("SELECT * FROM cardrecord limit 1 offset " + (rownum-1), null);
	    if (cur == null)
	    	db.close();
	    return cur;
	} 	
	
	public int GetRecordCount()
	{
	    SQLiteDatabase db = getWritableDatabase(); 
	    Cursor c = db.rawQuery("select count(*) from cardrecord", null);
	    c.moveToFirst();
	    m_iTotalRec = c.getInt(0);	
	    c.close();
	    return m_iTotalRec;
	}
	public Cursor query(int iPage){ 
		m_iCurPage = iPage;
	    //��ȡSQLiteDatabaseʵ��   
	    SQLiteDatabase db = getWritableDatabase(); 

	    m_iTotalPage = m_iTotalRec / m_iRecPerPage;
	    if ((m_iTotalRec % m_iRecPerPage) != 0)
	      m_iTotalPage = m_iTotalPage+1;
	    
	    if (m_iCurPage > m_iTotalPage-1)
	    	m_iCurPage = m_iTotalPage-1;

	    if (m_iCurPage < 0)
	    	m_iCurPage = 0;

		String szSql = "SELECT * FROM cardrecord limit " + 
				m_iRecPerPage + 
				" offset " + m_iCurPage*m_iRecPerPage;		    
	    return db.rawQuery(szSql, null);
	}
	
	//6��ɾ��   
	public void del(int id){  	      
        //��ȡSQLiteDatabase   
		SQLiteDatabase db = getWritableDatabase();  
        db.delete("cardrecord","id=?",new String[]{String.valueOf(id)}); 
        db.close();
	}  	
	
	//7�����·���   
	public void update(ContentValues values, int id){  	      
        //��ȡSQLiteDatabase   
		SQLiteDatabase db = getWritableDatabase();  
        db.update("cardrecord", values, "id=?", new String[]{String.valueOf(id)});   	
        db.close();
	}	
}
